'use strict';
angular.module('app.auth').controller('CarInfoCtrl', function ($scope, $location, $cookieStore, $http) {
        var $windos;
        var $waterT = angular.element('.water_temperature_box');
        var $temperature = angular.element('.temperature_date_box .temperature');
        var $rPointer = angular.element('.r_pointer');
        var $vPointer = angular.element('.v_pointer');
        var $oilBox = angular.element('.oil_box');
        var $oilContent = angular.element('.oil_content');
        var $rotateV;
        var $dateBox = angular.element('.date_box');
        var $nowTime = new Date();
        $dateBox.text($nowTime.toLocaleDateString());

        function waterTemperature(temperature) {
            console.log(temperature);
            if (temperature < 101) {
                console.log($waterT.height())

                // $waterT.height(temperature * ($waterT.height() / 100));
                $waterT.height(temperature);
                $temperature.text(temperature + '℃')
            } else {
                console.log('data error')
            }
        }

        function rSpeed(speed) {
            speed < 10 ? $rPointer.css('transform', "rotate(" + (speed * 36 - 54) + 'deg' + ")") : console.log('data error');
        }

        function vSpeed(speed) {
            if (speed < 260) {
                $windos = $(window).width();
                $windos > 768 ? $rotateV = 145 : $rotateV = 38;
                $vPointer.css('transform', "rotate(" + (speed * 1.2 + $rotateV) + 'deg' + ")");
                $('.v_speed span').eq(0).text(speed)
            } else {
                console.log('data error')
            }
        }

        function oilContentData(oil) {
            var $oilContentH = $oilContent.height();
            $oilContent.height(oil * $oilContentH);
            if (oil < 1.01 && oil > 0.25) {
                $oilBox.addClass('green_oil_box').removeClass('red_oil_box');
            } else if (oil < 0.251) {
                $oilBox.addClass('red_oil_box').removeClass('green_oil_box');
            } else {
                console.log('data error')
            }
        }

        waterTemperature(50);
        rSpeed(0);
        vSpeed(0);
        oilContentData(0.75);


// 百度地图API功能
    var map = new BMap.Map("findCar", {mapType: BMAP_HYBRID_MAP}); // 创建Map实例
    var centerPoint = new BMap.Point(108.946306, 34.347436);
    var local = new BMap.LocalSearch(map, {
        renderOptions: {map: map, panel: "r-result"}
    });
    var navigationControl = new BMap.NavigationControl({
        anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
        type: BMAP_NAVIGATION_CONTROL_LARGE,
        enableGeolocation: true
    });
    var geoc = new BMap.Geocoder();//地理代码

    var json_data = [[116.404, 39.915], [116.383752, 39.91334], [116.384502, 39.932241]];

    angular.element('.position_info_box').hide();
    function baiduMapSet(){
        map.centerAndZoom(new BMap.Point(116.404, 39.915), 5);// 设置地图的中心和缩放的等级
        console.log(centerPoint,'1');
        map.addControl(navigationControl);//添加地图缩放和方向控件
        map.addControl(new BMap.MapTypeControl());//添加地图浏览模式
        // map.enableScrollWheelZoom(true);//滚轮放大缩小
        // map.disableDragging();//禁止拖拽
        // setTimeout(function () {
        //     map.enableDragging();   // 开启拖拽
        // }, 300);
        // console.log(pointArray)
        // map.setViewport(pointArray);//让所有点在视野范围内
    }

    function addMarker() {
        var pointArray = new Array();
        for (var i = 0; i < json_data.length; i++) {
            var marker = new BMap.Marker(new BMap.Point(json_data[i][0], json_data[i][1])); // 创建点
            map.addOverlay(marker);    //增加点
            pointArray[i] = new BMap.Point(json_data[i][0], json_data[i][1]);
            marker.addEventListener("click", attribute);
        }

        console.log(json_data)

    }

    /*获取覆盖物位置*/
    function attribute(e) {
        var p = e.target;
        console.log("marker的位置是" + p.getPosition().lng + "," + p.getPosition().lat);
        $('.position_info_box').show();
        var jing_du_value = e.point.lng;
        var wei_du_value = e.point.lat;
        var pt = new BMap.Point(jing_du_value, wei_du_value);
        $("#spn1").text("经度:" + jing_du_value);
        $("#spn2").text("纬度:" + wei_du_value);
        console.log(e);
        var carPoint = e.point;
        geoc.getLocation(carPoint, function (rs) {
            //addressComponents对象可以获取到详细的地址信息
            var addComp = rs.addressComponents;
            var carSite;
            if (addComp.province === "") {
                carSite = "地址:" + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
            } else if (addComp.city === "") {
                carSite = "地址:" + addComp.province + addComp.district + addComp.street + addComp.streetNumber;
            } else if (addComp.district === "") {
                carSite = "地址:" + addComp.province + addComp.city + addComp.street + addComp.streetNumber;
            } else if (addComp.street === "") {
                carSite = "地址:" + addComp.province + addComp.city + addComp.district + addComp.streetNumber;
            } else if (addComp.streetNumber === "") {
                carSite = "地址:" + addComp.province + addComp.city + addComp.district + addComp.street;
            } else {
                carSite = "地址:" + addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
            }

            //将对应的HTML元素设置值
            console.log(carSite);
            console.log(carPoint);
            $(".position_info_box .car_addr").text(carSite);
        });
    }

    baiduMapSet();
    addMarker();



     var btn = document.getElementById('btn');
     var text = document.getElementById('search_ipt');
     btn.onclick = function () {
         var txt = text.value;
         local.search(txt);
     }

});